package com.ckzp.jfinal.system.util;

import com.ckzp.core.json.Jmode;
import com.ckzp.core.util.StringUtil;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.system.model.SysPost;
import com.ckzp.jfinal.system.model.SysRole;
import com.ckzp.jfinal.system.model.SysUser;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 51594 on 2022/7/28.
 */
public class SysUserUtil {

    public static SysUserUtil n() {
        return new SysUserUtil();
    }

    /**
     * 取得所有未删除的帐号
     *
     * @return
     */
    public List<SysUser> getActiveUserList() {
        return SysUser.dao.find("select * from sys_user where del_tag=0 order by emp_id");
    }

    /**
     * 取得岗位xselect数据列表
     *
     * @return
     */
    public List getSysPostList() {
        List<SysPost> postlist = SysPost.dao.find("select * from sys_post order by sindex");
        List list = new ArrayList();
        for (SysPost st : postlist) {
            int post_id = st.mod.getInt("post_id");
            String post_name = st.mod.getStr("post_name");
            list.add(Jmode.n().set("name", post_name).set("value", post_id));
        }
        return list;
    }

    /**
     * 取得用户的所有角色列表
     *
     * @param emp_id
     * @return
     */
    public String getUserRoleString(int emp_id) {
        List<SysRole> rolelist = SysRole.dao.find("select * from sys_user_role where emp_id=?", emp_id);
        StringBuffer sb = new StringBuffer("|");
        for (SysRole role : rolelist) {
            sb.append(role.getStr("role_id")).append("|");
        }
        if (sb.indexOf("|login|") == -1) sb.append("login").append("|");
        return sb.toString();
    }

    /**
     * @param role_id
     * @return
     */
    public FormSelect getRoleUserList(String role_id) {
        FormSelect fs = FormSelect.n().setName("emp_id");
        fs.setSource("select u.emp_id as id,u.user_name as name from sys_user u, sys_user_role r  where r.emp_id=u.emp_id and r.role_id='" + role_id + "'");
        return fs;
    }

    /**
     * 给用记有配置指定角色
     *
     * @param emp_id
     * @param role_id
     */
    public void saveUserRole(int emp_id, String role_id) {
        Db.update("delete from sys_user_role where role_id=? and emp_id=?", role_id, emp_id);
        Db.save("sys_user_role", new Record().set("role_id", role_id).set("emp_id", emp_id));
    }

    /**
     * @param posts
     * @return
     */
    public static String transPost(String posts) {
        posts = StringUtil.replace(posts, ",", "|");
        if (!posts.startsWith("|")) posts = "|" + posts;
        if (!posts.endsWith("|")) posts += "|";
        return posts;
    }


    /**
     * @param posts
     * @return
     */
    public static String unTransPost(String posts) {
        posts = StringUtil.replace(posts, "|", ",");
        if (posts.startsWith(",")) posts = posts.substring(1, posts.length());
        if (posts.endsWith(",")) posts = posts.substring(0, posts.length() - 1);
        return posts;
    }

}
